package com.sjb.sixteen_journey_backend.config;

import com.sjb.sixteen_journey_backend.interceptors.LoginInterceptor;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;


//解决跨域问题
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //跨域配置
        registry.addMapping("/**").allowedOriginPatterns("*").allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE", "PATCH").allowCredentials(true).allowedHeaders("*")
                .exposedHeaders("access-control-allow-origin").maxAge(3600);
    }

    //    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        //登录接口和注册接口不拦截
////        registry.addInterceptor(loginInterceptor);
//        registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login").excludePathPatterns("/doc.html").excludePathPatterns("/user/register");
//    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .excludePathPatterns("/**.html")
                .excludePathPatterns("/**.js")
                .excludePathPatterns("/v3/api-docs/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/register")
//                等同于
//                .excludePathPatterns("/v3/api-docs/swagger-config")
//                .excludePathPatterns("/v3/api-docs/admin-api")
//                .excludePathPatterns("/swagger-ui/oauth2-redirect.html")
        ;


    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/myImg/**").addResourceLocations("classpath:/resources/static/images/");
// 添加对favicon.ico的映射
        registry.addResourceHandler("/favicon.ico").addResourceLocations("file:"+"/favicon.ico");
//        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/static/images/favicon.ico");
        registry.addResourceHandler("/myImg/**").addResourceLocations("file:" + System.getProperty("user.dir") + "/src/main/resources/static/images/");
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(authInterceptorHandler()).excludePathPatterns("/**.html")
//                .excludePathPatterns("/**.js")
//                .excludePathPatterns("/v3/api-docs/**")
//                .excludePathPatterns("/user/login")
//                .excludePathPatterns("/user/register");
//    }
//
//    @Bean
//    public LoginInterceptor authInterceptorHandler() {
//        return new LoginInterceptor();
//    }

//    @Bean
//    TomcatServletWebServerFactory tomcatServletWebServerFactory() {
//        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() {
//            @Override
//            protected void postProcessContext(Context context) {
//                SecurityConstraint constraint = new SecurityConstraint();
//                constraint.setUserConstraint("CONFIDENTIAL");
//                SecurityCollection collection = new SecurityCollection();
//                collection.addPattern("/*");
//                constraint.addCollection(collection);
//                context.addConstraint(constraint);
//            }
//        };
//        factory.addAdditionalTomcatConnectors(createHttpsConnector());
//        return factory;
//    }
//
//
//    private Connector createHttpsConnector() {
//        // 设置http请求端口为8081的都自动重定向到https端口
//        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//        connector.setScheme("http");
//        connector.setPort(8081);
//        connector.setSecure(false);
//        connector.setRedirectPort(8080);
//        return connector;
//    }
}